package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User findUsersAndAuthoritiesByUsername(String username) {

        //根据用户名，查询用户
       User user=userMapper.findUserByUsername(username);
       //根据用户id查询用户角色
        Set<Role> roleSet=userMapper.findRolesByUserId(user.getId());

        //循环遍历角色表
        for (Role role : roleSet) {
            Set<Permission> permissionsSet=userMapper.findPermissionsByRoleId(role.getId());
            role.setPermissions(permissionsSet);
        }
        user.setRoles(roleSet);
        return user;
    }
}
